package com.base.config

import org.redisson.Redisson
import org.redisson.api.RedissonClient
import org.redisson.config.Config
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
class RedissonConfig {
    @Value("\${spring.redis.host}")
    private val host: String? = null

    @Value("\${spring.redis.port}")
    private val port: String? = null

    @Value("\${spring.redis.password}")
    private val redisPassword: String? = null

    @get:Bean
    val redisson: RedissonClient?
        get() {
            if (host.isNullOrBlank() || port.isNullOrBlank() || redisPassword.isNullOrBlank()) return null
            val config = Config()
            //单机模式  依次设置redis地址和密码
            config.useSingleServer().setAddress("redis://$host:$port").password = redisPassword
            //集群模式
//            config.useClusterServers()
//                    .addNodeAddress("redis://$host:$port")
//                    .password = redisPassword
            return Redisson.create(config)
        }
}
